package indi.kider.server.applet.service;

import indi.kider.common.constant.RentStateCons;
import indi.kider.common.model.apabus.Rent;
import indi.kider.common.model.bill.Bill;
import indi.kider.server.apabus.mapper.RentMapper;
import indi.kider.server.bill.mapper.BillMapper;
import indi.kider.server.bill.model.BillInfoModel;
import indi.kider.server.bill.model.UnpayBillModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 未缴费app相关的接口实现
 *
 * @author wangjingjing
 * @create 2018-10-20 下午1:09
 **/
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
@Slf4j
public class PayBillServiceImpl implements PayBillService {

    @Autowired
    private BillMapper billMapper;

    @Autowired
    private RentMapper rentMapper;

    @Override
    public UnpayBillModel getUnpayBillModel(Long tenatId) {
        Rent rent = new Rent();
        rent.setTenantId(tenatId);
        rent.setState(RentStateCons.VALID);
        Rent rentx = rentMapper.selectOne(rent);

        Long   rentId   = rentx.getId();
        String roomNumb = rentx.getHouseNumb();
        String month    = LocalDate.now().minusMonths(1).format(DateTimeFormatter.ofPattern("yyyyMM"));

        UnpayBillModel unpayBillModel = new UnpayBillModel();

        unpayBillModel.setDetail(billMapper.getOwnUnpayBillModeDetail(rentId));
        BillInfoModel       roomBill = new BillInfoModel();
        BillInfoModel       ownBill  = new BillInfoModel();
        List<BillInfoModel> rblist   = billMapper.getRootBillMode(roomNumb, month);
        List<BillInfoModel> oblist   = billMapper.getOwnUnpayBillMode(rentId);
        if (!(rblist == null || rblist.isEmpty())) {
            roomBill = rblist.get(0);
        }

        if (!(oblist == null || oblist.isEmpty())) {
            ownBill = oblist.get(0);
        }
        unpayBillModel.setRoomBill(roomBill);
        unpayBillModel.setOwnBill(ownBill);

        Bill bill = new Bill();
        bill.setPayState(0);
        bill.setBillAttribution(rentId);

        unpayBillModel.setPayInfoList(billMapper.select(bill));
//        List<Bill> bs = new ArrayList<>();
//        Bill testBi = new Bill();
//        testBi.setBillAttribution(1l);
//        testBi.setTotal((float) 300l);
//        bs.add(testBi);
//        unpayBillModel.setPayInfoList(bs);
//        List<BillInfoModel> detail = new ArrayList<>();
//        BillInfoModel billInfoModel  = new BillInfoModel();
//        billInfoModel.setPowerBill((float) 200l);
//        billInfoModel.setWaterBill((float) 50l);
//
//        billInfoModel.setBillData("201809");
//        detail.add(billInfoModel);
//
//        unpayBillModel.setDetail(detail);
//        unpayBillModel.setRoomBill(billInfoModel);
//        unpayBillModel.setOwnBill(billInfoModel);

        return unpayBillModel;
    }

}
